package com.rockcent.wechat;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.JsonResult;
import com.rockcent.common.utils.RequestUtils;
import com.rockcent.mall.lib.domain.Mall;
import com.rockcent.mall.lib.dto.CustomerDto;
import com.rockcent.mall.lib.repository.MallRepository;
import com.rockcent.wechat.helper.CustomerHelper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by oyhk on 15/11/29.
 *
 */
@Component
public class DomainInterceptor extends HandlerInterceptorAdapter {

    private final Logger log = LoggerFactory.getLogger(DomainInterceptor.class);

    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private Prop prop;
    @Autowired
    private MallRepository mallRepository;
    @Autowired
    private CustomerHelper customerHelper;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        CustomerDto customerDto = customerHelper.getSession(request);
        if (customerDto == null) {
            customerDto = new CustomerDto();
        }
        if (customerDto.getMallId() != null) {
            return true;
        }

        String domain = request.getHeader("domain");
        log.info("======================>domain : {}", domain);
        if(StringUtils.isBlank(domain)){
            domain = RequestUtils.getStringParameter(request,"domain");
        }

        if (StringUtils.isBlank(domain)) {
            response.getWriter().print(objectMapper.writeValueAsString(new JsonResult<>("10011", "平台C端地址 domain 不能为空")));
            response.getWriter().close();
            log.warn("======================>code : {}, desc : {} ", 10011, "平台C端地址 domain 不能为空",log);
            return false;
        }
        log.info("DomainInterceptor domain:{}",domain);
        Mall mall = mallRepository.findByMobileHostName(domain); // quancome.c.rockcent.com
//        log.warn("======================>findByMobileHostName : {}", mall);
        if (mall == null) {
            mall = mallRepository.findByCustomerDomain(domain); // 自定义域名 例如:m.quancome.com
//            log.warn("======================>findByCustomerDomain : {}", mall);
        }
        if (mall == null) {
            response.getWriter().print(objectMapper.writeValueAsString(new JsonResult<>("10012", "平台C端地址 domain 不存在")));
            response.getWriter().close();
            log.warn("======================>code : {}, desc : {} ", 10012, "平台C端地址 domain 不存在");
            return false;
        }
        customerDto.setMallId(mall.getId());
        customerDto.setMallCustomerDomain(domain);
        request.getSession().setAttribute(prop.customerLoginName, customerDto);

        return super.preHandle(request, response, handler);
    }
}
